package com.keke.myblogv1.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keke.myblogv1.article.dto.QueryArticleByPageDTO;
import com.keke.myblogv1.article.entity.MyBlogArticle;
import com.keke.myblogv1.article.mapper.MyBlogArticleMapper;
import com.keke.myblogv1.article.service.MyBlogArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keke.myblogv1.updatelog.entity.MyBlogUpdateLog;
import com.keke.myblogv1.updatelog.mapper.MyBlogUpdateLogMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Derrick
 * @since 2022-07-31
 */
@Service
public class MyBlogArticleServiceImpl extends ServiceImpl<MyBlogArticleMapper, MyBlogArticle> implements MyBlogArticleService {

    @Resource
    private MyBlogArticleMapper myBlogArticleMapper;

    @Resource
    private MyBlogUpdateLogMapper myBlogUpdateLogMapper;

    @Override
    public Page<MyBlogArticle> getAllArticleByPage(QueryArticleByPageDTO queryArticleByPageDTO) {
        Page<MyBlogArticle> page = new Page<>(queryArticleByPageDTO.getPageNum(), queryArticleByPageDTO.getPageSize());
        LambdaQueryWrapper<MyBlogArticle> query = new LambdaQueryWrapper<>();
        query.eq(MyBlogArticle::getStatus, "1");
        query.orderByDesc(MyBlogArticle::getCreateDate);
        Page<MyBlogArticle> myBlogArticlePage = myBlogArticleMapper.selectPage(page, query);
        return myBlogArticlePage;
    }

    @Override
    public MyBlogArticle getAllArticleById(int id) {
        LambdaQueryWrapper<MyBlogArticle> myBlogArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        myBlogArticleLambdaQueryWrapper.eq(MyBlogArticle::getId, id)
                .eq(MyBlogArticle::getStatus, "1");
        MyBlogArticle myBlogArticle = myBlogArticleMapper.selectOne(myBlogArticleLambdaQueryWrapper);
        return myBlogArticle;
    }

    @Override
    public boolean insertArticle(MyBlogArticle myBlogArticle) {
        myBlogArticle.setCreateDate(new Date());
        myBlogArticle.setCreateBy("admin");
        myBlogArticle.setType(1);
        int insertArticle = myBlogArticleMapper.insert(myBlogArticle);

        MyBlogUpdateLog myBlogUpdateLog = new MyBlogUpdateLog();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = sdf.format(date);
        myBlogUpdateLog.setUpdateDate(date);
        myBlogUpdateLog.setUpdateBy("admin");
        myBlogUpdateLog.setInfo("添加文章: "+myBlogArticle.getTitle());
        myBlogUpdateLog.setContent(myBlogUpdateLog.getUpdateBy()+" 添加于" + format);


        int insertLog = myBlogUpdateLogMapper.insert(myBlogUpdateLog);
        if(insertArticle>0&&insertLog>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public boolean updateArticle(MyBlogArticle myBlogArticle) {
        myBlogArticle.setUpdateBy("admin");
        myBlogArticle.setUpdateDate(new Date());
        int insertArticle = myBlogArticleMapper.updateById(myBlogArticle);

        MyBlogUpdateLog myBlogUpdateLog = new MyBlogUpdateLog();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = sdf.format(date);
        myBlogUpdateLog.setUpdateDate(date);
        myBlogUpdateLog.setUpdateBy("admin");
        myBlogUpdateLog.setInfo("更新文章: "+myBlogArticle.getTitle());
        myBlogUpdateLog.setContent(myBlogUpdateLog.getUpdateBy()+" 更新于" + format);
        int insertLog = myBlogUpdateLogMapper.insert(myBlogUpdateLog);

        if(insertArticle>0&&insertLog>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deleteArticle(MyBlogArticle myBlogArticle) {
        myBlogArticle.setStatus("0");
        myBlogArticle.setUpdateBy("admin");
        myBlogArticle.setUpdateDate(new Date());
        int insertArticle = myBlogArticleMapper.updateById(myBlogArticle);

        MyBlogUpdateLog myBlogUpdateLog = new MyBlogUpdateLog();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = sdf.format(date);
        myBlogUpdateLog.setUpdateDate(date);
        myBlogUpdateLog.setUpdateBy("admin");
        myBlogUpdateLog.setInfo("删除文章: "+myBlogArticle.getTitle());
        myBlogUpdateLog.setContent(myBlogUpdateLog.getUpdateBy()+" 删除于" + format);
        int insertLog = myBlogUpdateLogMapper.insert(myBlogUpdateLog);

        if(insertArticle>0&&insertLog>0){
            return true;
        }else{
            return false;
        }
    }


}
